package com.yiren.backstage.judge.services.light.impl;

import android.content.Context;

import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.enums.ResultStateEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.judge.handler.LightJudgeHandler;
import com.yiren.backstage.judge.services.light.ILightJudgeThreadService;
import com.yiren.backstage.judge.services.light.ILightStateCheckService;
import com.yiren.backstage.player.api.IPlayerCallBack;
import com.yiren.backstage.player.api.PlayerApi;

/**
 * Time:2022/3/23 16:02
 * Author:xiaozan
 * Description:灯光评判线程 服务实现
 */
public class LightJudgeThreadServiceImpl implements ILightJudgeThreadService {

    private Context context;

    /*** 开始状态*/
    private CarStateBean startCarState;

    private LightJudgeHandler lightJudgeHandler;

    private ILightStateCheckService iLightStateCheckService;

    /*** 是否正在播报 */
    private boolean isPlay;

    public LightJudgeThreadServiceImpl(Context context, LightJudgeHandler lightJudgeHandler) {
        this.context = context;
        this.lightJudgeHandler = lightJudgeHandler;
        iLightStateCheckService = new LightStateCheckServiceImpl();
    }

    public ResultStateEnum playLightContent(String content) {
        try {
            isPlay = true;
            startCarState = null;
            PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(content, ""), new IPlayerCallBack() {
                @Override
                public void playFinish() {
                    isPlay = false;
                }
            });
            //等待播报完成
            while (isPlay && lightJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                //判断是否抢语音操作
                if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D9.toString())) {
                    if (robVoiceOperation()) return ResultStateEnum.FAIL;
                }
                Thread.sleep(10);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultStateEnum.SUCCESS;
    }

    @Override
    public ResultStateEnum waitOperationResult(SingleLightEntity singleLightEntity, long mStartTime) {
        boolean accordCarState = iLightStateCheckService.stateIsAccord(singleLightEntity.getStateValue());
        boolean accordOvertime = (System.currentTimeMillis() - mStartTime) / 1000 >= singleLightEntity.getWaitTime();
        //灯光操作为空时 超时后返回成功结果
        if (singleLightEntity.getStateValue().equals("{}")) {
            return accordOvertime ? ResultStateEnum.SUCCESS : ResultStateEnum.EXECUTING;
        }
        //超时 并且 不符合状态
        if (accordOvertime && !accordCarState) {
            return ResultStateEnum.FAIL;
        }
        //未超时 并且 符合状态
        if (!accordOvertime && accordCarState) {
            return ResultStateEnum.SUCCESS;
        }
        return ResultStateEnum.EXECUTING;
    }

    @Override
    public ResultStateEnum keepOperationResult(SingleLightEntity singleLightEntity, long accordCarStateStartTime) {
        try {
            boolean accordCarState = iLightStateCheckService.stateIsAccord(singleLightEntity.getStateValue());
            float waitTime = (float) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D8.toString());
            if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D11.toString())) {
                if ((System.currentTimeMillis() - accordCarStateStartTime) <= waitTime * 1000) {
                    //这等待时间内状态不符合就扣分
                    if (!accordCarState) {
                        return ResultStateEnum.FAIL;
                    }
                } else {
                    return ResultStateEnum.SUCCESS;
                }
            } else {
                //休眠时间
                int i = 0;
                int count = (int) ((waitTime * 1000) / 100);
                while (lightJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE && i <= count) {
                    Thread.sleep(100);
                    i++;
                }
                return ResultStateEnum.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultStateEnum.EXECUTING;
    }

    @Override
    public boolean robVoiceOperation() {
        CarStateBean realCarState = CarStateBean.getInstance();
        if (null == startCarState) {
            startCarState = new CarStateBean();
            startCarState.setDrivingLamp(realCarState.getDrivingLamp());
            startCarState.setPassingLight(realCarState.getPassingLight());
            startCarState.setDistanceLight(realCarState.getDistanceLight());
            startCarState.setLeftLight(realCarState.getLeftLight());
            startCarState.setRightLight(realCarState.getRightLight());
            startCarState.setFoglight(realCarState.getFoglight());
        }

        //判断开始灯光状态是否改变
        if (startCarState.getDrivingLamp() == realCarState.getDrivingLamp()
                && startCarState.getPassingLight() == realCarState.getPassingLight()
                && startCarState.getDistanceLight() == realCarState.getDistanceLight()
                && startCarState.getLeftLight() == realCarState.getLeftLight()
                && startCarState.getRightLight() == realCarState.getRightLight()
                && startCarState.getFoglight() == realCarState.getFoglight()) {
            return false;
        }
        return true;
    }
}
